import mapUtils from "@/plugins/esri/mapUtils";
import { fetchConfig } from "@/api/common";

export default {
    data() {
        return {
            carListenerConfig: {},
            carMarkers: [],
        };
    },
    methods: {
        startCarListener() {
            // 启动ws
            fetchConfig().then(config => {
                this.carListenerConfig.url = config.MAP_CAR_WS_URL;
                console.log("MapCarListener:", "初始化WebSocket连接", "连接地址：", this.carListenerConfig.url);
                this.carListenerConfig.ws = new WebSocket(this.carListenerConfig.url);
                this.startWebsocket();
            });
        },
        startWebsocket() {
            this.carListenerConfig.ws.onopen = event => {
                // 建立连接
                console.log("MapCarListener:", "已建立连接");
                this.carListenerConfig.ws.onmessage = event => {
                    let data = JSON.parse(event.data);
                    if (data.messageType && data.messageType == "position") {
                        this.renderCars(data.data);
                    }
                };
            };
        },
        renderCars(data) {
            console.log(data);
            data.forEach(async item => {
                let point = await mapUtils.buildPointWithWkid([item.y, item.x], this.mapView.spatialReference);
                mapUtils.transformCoordinate(point, 4326).then(res => {
                    // 添加车辆标记
                    mapUtils.buildCarPoint({ center: res, attributes: { carId: item.amosCarInfo.carId }, label: item.amosCarInfo.carName }).then(markers => {
                        // 判断marker是否存在集合中
                        markers.forEach(marker => {
                            let hasMarker = this.carMarkers.findIndex(m => {
                                return m.attributes.carId == marker.attributes.carId && m.attributes.tag == marker.attributes.tag;
                            });
                            if (hasMarker >= 0) {
                                // 存在，移除
                                this.mapTool.removeMarker(this.carMarkers[hasMarker]);
                                this.carMarkers.splice(hasMarker, 1);
                            }
                            this.carMarkers.push(marker);
                            this.mapTool.addLabelIconMarker([marker]);
                        });
                    });
                });
            });
        },
    },
};

// {"data":[{"amosCarInfo":{"carId":"900","carModeNo":"1","carName":"一汽大众","carTypeNo":"1","color":"白色","fuelType":"95#","manufacturer":"1"},"kctm":"20241010150003","onln":"1","ori":"150","sped":"0.07","stat":"0","vhid":"900","vhno":"F1433","vpid":"96728834","x":"35348.6","y":"67179.63"},{"amosCarInfo":{"carId":"900","carModeNo":"1","carName":"一汽大众","carTypeNo":"1","color":"白色","fuelType":"95#","manufacturer":"1"},"kctm":"20241010150003","onln":"1","ori":"150","sped":"0.07","stat":"0","vhid":"900","vhno":"F1433","vpid":"96728834","x":"35348.6","y":"67179.63"}],"messageType":"position"}
